//
//  HomeViewModel.m
//  NekaPrice
//
//  Created by yu on 2018/6/11.
//  Copyright © 2018年 ma c. All rights reserved.
//

#import "HomeViewModel.h"
#import "NSString+Size.h"
@implementation HomeViewModel

- (instancetype)init {
    if (self = [super init]) {
        _firstModel = [[ProductModel alloc] initWithInitalRowCount:3];
        _secondModel = [[ProductModel alloc] initWithInitalRowCount:3];
        _thirdModel = [[ProductModel alloc] initWithInitalRowCount:4];
    }
    return self;
}


- (void)requestBrandList:(void (^)(void))finished {
  
    dispatch_group_t downloadGroup = dispatch_group_create();
    
    //漆面保护膜--汽车品牌
    dispatch_group_enter(downloadGroup);
    [[NKNetWorkTool sharedTool] requestGET:FormURL(API_PROTECT_CAR_LIST) parames:nil success:^(id responseObj) {
        NSLog(@"%@", responseObj);
        NSArray *array = responseObj[@"brand_list"];
        self.firstBrandList = [NSMutableArray new];
        for (NSDictionary *dic in array) {
            [self.firstBrandList addObject:[CarModel ModelWithDic:dic]];
        }
        dispatch_group_leave(downloadGroup);
    } failure:^(id error) {
        NSLog(@"%@", error);
        dispatch_group_leave(downloadGroup);
    }];
    //漆面保护膜--项目分类
    dispatch_group_enter(downloadGroup);
    [[NKNetWorkTool sharedTool] requestGET:FormURL(API_PROTECT_PRO_LIST) parames:nil success:^(id responseObj) {
        NSLog(@"%@", responseObj);
        
        NSArray *array = responseObj[@"inv_product_list"];
        self.firstProList = [NSMutableArray new];
        for (NSDictionary *dic in array) {
            [self.firstProList addObject:[ProjectModel ModelWithDic:dic]];
        }
        self.firstModel.carProductModel = self.firstProList[0];
        dispatch_group_leave(downloadGroup);
    } failure:^(id error) {
        NSLog(@"%@", error);
        dispatch_group_leave(downloadGroup);
    }];
    
    
    
    //个性改色膜--汽车品牌
    dispatch_group_enter(downloadGroup);
    [[NKNetWorkTool sharedTool] requestGET:FormURL(API_COLOR_CAR_LIST) parames:nil success:^(id responseObj) {
        
        NSLog(@"%@", responseObj);
        
        NSArray *array = responseObj[@"brand_list"];
        self.secondBrandList = [NSMutableArray new];
        for (NSDictionary *dic in array) {
            [self.secondBrandList addObject:[CarModel ModelWithDic:dic]];
        }
        dispatch_group_leave(downloadGroup);
    } failure:^(id error) {
        NSLog(@"%@", error);
        dispatch_group_leave(downloadGroup);
    }];
    
    //个性改色膜--项目分类
    dispatch_group_enter(downloadGroup);
    [[NKNetWorkTool sharedTool] requestGET:FormURL(API_COLOR_PRO_LIST) parames:nil success:^(id responseObj) {
        NSLog(@"%@", responseObj);
        
        NSArray *array = responseObj[@"product_list"];
        self.secondProList = [NSMutableArray new];
        for (NSDictionary *dic in array) {
            [self.secondProList addObject:[ProjectModel ModelWithDic:dic]];
        }
        self.secondModel.carProductModel = self.secondProList[0];
        dispatch_group_leave(downloadGroup);
    } failure:^(id error) {
        NSLog(@"%@", error);
        dispatch_group_leave(downloadGroup);
    }];
    
    //安全隔热膜--汽车品牌
    dispatch_group_enter(downloadGroup);
    [[NKNetWorkTool sharedTool] requestGET:FormURL(API_GLASS_CAR_LIST) parames:nil success:^(id responseObj) {
   
        NSLog(@"%@", responseObj);
        
        NSArray *array = responseObj[@"brand_list"];
        self.thirdBrandList = [NSMutableArray new];
        for (NSDictionary *dic in array) {
            [self.thirdBrandList addObject:[CarModel ModelWithDic:dic]];
        }
        dispatch_group_leave(downloadGroup);
    } failure:^(id error) {
        NSLog(@"%@", error);
        dispatch_group_leave(downloadGroup);
    }];
    
    //安全隔热膜--施工部位
    dispatch_group_enter(downloadGroup);
    [[NKNetWorkTool sharedTool] requestGET:FormURL(API_GLASS_PART_LIST) parames:nil success:^(id responseObj) {
       
        NSLog(@"%@", responseObj);
        
        NSArray *array = responseObj[@"pac_product_list"];
        self.thirdPartList = [NSMutableArray new];
        for (NSDictionary *dic in array) {
            [self.thirdPartList addObject:[ProjectModel ModelWithDic:dic]];
        }
        self.thirdModel.glaPartModel = self.thirdPartList[0];
        dispatch_group_leave(downloadGroup);
    } failure:^(id error) {
        NSLog(@"%@", error);
        dispatch_group_leave(downloadGroup);
    }];
    
    //安全隔热膜--项目分类
    dispatch_group_enter(downloadGroup);
    //默认部位(全车)gla_fid = 1
    [[NKNetWorkTool sharedTool] requestGET:FormURL(API_GLASS_PRO_LIST) parames:@{@"gla_fid": @"1"} success:^(id responseObj) {
 
        NSLog(@"%@", responseObj);
        
        NSArray *array = responseObj[@"gla_product_list"];
        self.thirdProList = [NSMutableArray new];
        for (NSDictionary *dic in array) {
            [self.thirdProList addObject:[ProjectModel ModelWithDic:dic]];
        }
        self.thirdModel.glaProductModel = self.thirdProList[0];
        dispatch_group_leave(downloadGroup);
    } failure:^(id error) {
        NSLog(@"%@", error);
        dispatch_group_leave(downloadGroup);
    }];
    
    dispatch_group_notify(downloadGroup, dispatch_get_main_queue(), ^{
        finished();
    });
    
}

//保护膜--获取车系列表
- (void)requestFirstSeriesList:(void (^)(void))finished {
    
    [[NKNetWorkTool sharedTool] requestGET:FormURL(API_CAR_SERIES_LIST) parames:@{@"cat_fid": self.firstModel.carBrandModel.cat_id} success:^(id responseObj) {
        
        NSLog(@"%@", responseObj);
        NSArray *arr = responseObj[@"category_list"];
        self.firstSeriesList = [NSMutableArray new];
        for (NSDictionary *dic in arr) {
            CarModel *model = [CarModel ModelWithDic:dic];
            [self.firstSeriesList addObject:model];
        }
        self.firstModel.carSeriesModel = self.firstSeriesList[0];
        finished();
        
    } failure:^(id error) {
        
    }];
}

//改色膜--获取车系列表
- (void)requestSecondSeriesList:(void (^)(void))finished {

    [[NKNetWorkTool sharedTool] requestGET:FormURL(API_CAR_SERIES_LIST) parames:@{@"cat_fid": self.secondModel.carBrandModel.cat_id} success:^(id responseObj) {
        
        NSLog(@"%@", responseObj);
        NSArray *arr = responseObj[@"category_list"];
        self.secondSeriesList = [NSMutableArray new];
        for (NSDictionary *dic in arr) {
            CarModel *model = [CarModel ModelWithDic:dic];
            [self.secondSeriesList addObject:model];
        }
        self.secondModel.carSeriesModel = self.secondSeriesList[0];
        finished();
        
    } failure:^(id error) {
        
    }];
    
}

//隔热膜--获取车系列表
- (void)requestThirdSeriesList:(void (^)(void))finished {
    
    [[NKNetWorkTool sharedTool] requestGET:FormURL(API_CAR_SERIES_LIST) parames:@{@"cat_fid": self.thirdModel.carBrandModel.cat_id} success:^(id responseObj) {
        NSLog(@"%@", responseObj);
        NSArray *arr = responseObj[@"category_list"];
        self.thirdSeriesList = [NSMutableArray new];
        for (NSDictionary *dic in arr) {
            CarModel *model = [CarModel ModelWithDic:dic];
            [self.thirdSeriesList addObject:model];
        }
        self.thirdModel.carSeriesModel = self.thirdSeriesList[0];
        finished();
    } failure:^(id error) {
        
    }];
}

//隔热膜--针对选择的施工部位,获取支持的项目列表
- (void)requestThirdProjectList:(void (^)(void))finished {
    
    [[NKNetWorkTool sharedTool] requestGET:FormURL(API_GLASS_PRO_LIST) parames:@{@"gla_fid": self.thirdModel.glaPartModel.gla_id} success:^(id responseObj) {
        
        NSArray *array = responseObj[@"gla_product_list"];
        self.thirdProList = [NSMutableArray new];
        for (NSDictionary *dic in array) {
            [self.thirdProList addObject:[ProjectModel ModelWithDic:dic]];
        }
        self.thirdModel.glaProductModel = self.thirdProList[0];
        finished();
        
    } failure:^(id error) {
        
    }];

}


- (void)requestPrice:(NSInteger)index {
    if (index == 0) {
        if (self.firstModel.carSeriesModel.cat_id && self.firstModel.carProductModel.pro_id) {
            NSDictionary *parames = @{@"cat_id": self.firstModel.carSeriesModel.cat_id,
                                      @"inv_id": self.firstModel.carProductModel.inv_id};
            [[NSNotificationCenter defaultCenter] postNotificationName:@"SHOW_PROGRESS" object:nil];
            [[NKNetWorkTool sharedTool] requestGET:FormURL(API_PROTECT_PRICE) parames:parames success:^(id responseObj) {
                
                NSString *price = responseObj[@"price"];
                self.firstModel.price = price;
                if (self.resultBlock) {
                    self.resultBlock();
                }
            } failure:^(id error) {
                
            }];
        }
    }else if (index == 1) {
        if (self.secondModel.carSeriesModel.cat_id && self.secondModel.carProductModel.pro_id) {
            NSDictionary *parames = @{@"cat_id": self.secondModel.carSeriesModel.cat_id,
                                      @"pro_id": self.secondModel.carProductModel.pro_id};
            [[NSNotificationCenter defaultCenter] postNotificationName:@"SHOW_PROGRESS" object:nil];
            [[NKNetWorkTool sharedTool] requestGET:FormURL(API_COLOR_PRICE) parames:parames success:^(id responseObj) {
                
                NSString *price = responseObj[@"price"];
                self.secondModel.price = price;
                if (self.resultBlock) {
                    self.resultBlock();
                }
            } failure:^(id error) {
                
            }];
        }
    }else {
        
        if (self.thirdModel.carSeriesModel.cat_id && self.thirdModel.glaProductModel.gla_id) {
            NSDictionary *parames = @{@"cat_id": self.thirdModel.carSeriesModel.cat_id,
                                      @"gla_id": self.thirdModel.glaProductModel.gla_id};
            [[NSNotificationCenter defaultCenter] postNotificationName:@"SHOW_PROGRESS" object:nil];
            [[NKNetWorkTool sharedTool] requestGET:FormURL(API_GLASS_PRICE) parames:parames success:^(id responseObj) {
                NSLog(@"%@", responseObj);
                NSString *price = responseObj[@"price"];
                self.thirdModel.price = price;
                if (self.resultBlock) {
                    self.resultBlock();
                }
            } failure:^(id error) {
                
            }];
        }
        
    }
}


- (CGFloat)detailHeightWithIndex:(NSInteger)index {
    NSDictionary *fontAtt = @{NSFontAttributeName: [UIFont systemFontOfSize:14]};
    if (index == 0 || index == 1) {
        NSString *str = @"根据您选择的车型，如因特殊原因需要更换局部的情况，请参照以下比例付费。";
        NSLog(@"%lf", [str heightWithFixedWidth:K_WIDTH - 40 andEdgeInsets:UIEdgeInsetsMake(5, 0, 5, 0) attributes:fontAtt]);
        return [str heightWithFixedWidth:K_WIDTH - 40 andEdgeInsets:UIEdgeInsetsMake(5, 0, 5, 0) attributes:fontAtt];
    }else {
        NSString *str = @"局部价格计算方式：\n全车套餐不包含天窗，以下为局部价格计算方式，以侧后价格为100%计算，（例如：普通天窗价格=该车型侧后报价 * 1/6）。";
        return [str heightWithFixedWidth:K_WIDTH - 40 andEdgeInsets:UIEdgeInsetsMake(5, 0, 5, 0) attributes:fontAtt];
    }
}



@end
